//
//  ViewController.m
//  map
//
//  Created by boc on 15/11/18.
//  Copyright © 2015年 bocweb. All rights reserved.
//

#import "ViewController.h"
#import "AppDelegate.h"
#import <UIKit/UIView.h>
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件

#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件

#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件

#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件

#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件

#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件

#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件
@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    _mapView = [[BMKMapView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    [_mapView setCenterCoordinate:CLLocationCoordinate2DMake(30.77,120.76)];
    _mapView.mapType=BMKMapTypeStandard;
    
    self.view = _mapView;
    [self makeNavitem];



}
-(void)location
{
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
    _mapView.showsUserLocation = NO;//先关闭显示的定位图层
    _mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;//设置定位的状态
    _mapView.showsUserLocation = YES;//显示定位图层
    BMKCoordinateRegion region;
    region.center.latitude  = userLocation.location.coordinate.latitude;
    region.center.longitude = userLocation.location.coordinate.longitude;
    region.span.latitudeDelta  = 0.2;
    region.span.longitudeDelta = 0.2;
    _mapView.region =region;


    [_mapView updateLocationData:userLocation];
    
}
-(void)makeNavitem
{
    UINavigationBar *navigationgBar = [[UINavigationBar alloc]init];

    UIBarButtonItem *customRightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"显示" style:UIBarButtonItemStylePlain target:self action:@selector(traffic)];
    [customRightBarButtonItem setTintColor:[UIColor blueColor]];

    [self.navigationItem setRightBarButtonItem:customRightBarButtonItem];
    UIBarButtonItem *customLeftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"定位" style:UIBarButtonItemStylePlain target:self action:@selector(location)];
    [customLeftBarButtonItem setTintColor:[UIColor blueColor]];
    
    [self.navigationItem setLeftBarButtonItem:customLeftBarButtonItem];
    [self.view addSubview:navigationgBar];
}
    
-(void)traffic
{
    [_mapView setTrafficEnabled:YES];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    _locService.delegate = self;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
    _locService.delegate = nil;
}
- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"BMKMapView控件初始化完成" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
    [alert show];
    alert = nil;
}

- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate {
    NSLog(@"map view: click blank");
}

- (void)mapview:(BMKMapView *)mapView onDoubleClick:(CLLocationCoordinate2D)coordinate {
    NSLog(@"map view: double click");
}
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    NSLog(@"heading is %@",userLocation.heading);
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    BMKUserLocation *_userLocation = userLocation;
//    _mapView.centerCoordinate = userLocation.location.coordinate;
    [_mapView updateLocationData: _userLocation];
//
    NSLog(@"didUpdateUserLocation lat %f,long %f",_userLocation.location.coordinate.latitude,_userLocation.location.coordinate.longitude);
}

@end


